package leetcode100;

import java.util.*;

public class lc002 {

    public static void main(String[] args) {
        StringJoiner sj = new StringJoiner(",");
        sj.add("tea").add("cat").add("eat");
        String[] strs = sj.toString().split(",");
        List<List<String>> res = groupAnagrams(strs);
        System.out.println(res);
    }


    public static List<List<String>> groupAnagrams(String[] strs) {
        char[] chars = null;
        HashMap<String, List<String>> map = new HashMap<>();
        for (String s : strs) {
            chars = s.toCharArray();
            Arrays.sort(chars);
            String sortedString = new String(chars);

            /*
                如果不存在，put，value是ArrayList，返回value，调用add
                如果存在，get，返回value,调用add
             */
            map.computeIfAbsent(sortedString, value -> new ArrayList<>()).add(s);
        }

        return new ArrayList<>(map.values());
    }


}
